﻿layui.define('layer', function (exports) {
    "use strict";
    var $ = layui.jquery,
        layerTips = parent.layer === undefined ? layui.layer : parent.layer;


    var baajax = {
        // post: function (url, params, callback) {
        //     $.post(url, params, function (res) {
        //         if (res.statusCode === 101) {
        //             layerTips.alert(res.msg, {
        //                 icon: 2, title: '系统提示', cancel: function (index, layero) {
        //                     top.location.href = location.origin + '/login';
        //                 }
        //             }, function () {
        //                 top.location.href = location.origin + '/login';
        //             });
        //         }
        //         callback(res);
        //     }, 'json');
        // },
        // get: function (url, params, callback) {
        //     $.getJSON(url, params, function (res) {
        //         if (res.statusCode === 101) {
        //             layerTips.alert(res.msg, {
        //                 icon: 2, title: '系统提示', cancel: function (index, layero) {
        //                     top.location.href = location.origin + '/login';
        //                 }
        //             }, function () {
        //                 top.location.href = location.origin + '/login';
        //             });
        //         }
        //         callback(res);
        //     });
        // },
        v: '1.0.0',
        /**
         * 向服务器发送数据
         * @param svr       请求服务名称，字符串类型
         * @param paras     请求参数，object{cmd:"",exp:"",tabname:""}
         * @param data      返回数据，json
         * @param callFunc  回调函数，function
         */
        post: function (svr, paras, callFunc, errFunc) {
            $.ajax({
                type: "post",
                url: svr,
                dataType: "json",
                data: paras,
                cache: true,
                success: function (data) {
                    callFunc(data);
                },
                error: function () {
                    errFunc();
                }
            })
        }
            ,postContentType: function (svr, paras, callFunc, errFunc) {
                $.ajax({
                    type: "post",
                    url: svr,
                    dataType: "json",
                    contentType:"application/json",
                    data: paras,
                    cache: true,
                    success: function (data) {
                        callFunc(data);
                    },
                    error: function () {
                        errFunc();
                    }
                })
            }




        /**
         * 向服务器请求数据
         * @param svr       请求服务名称，字符串类型
         * @param paras     请求参数，object{cmd:"",exp:"",tabname:""}
         * @param data      返回数据，json
         * @param callFunc  回调函数，function
         */
        , get: function (svr, paras, succFunc, errorFunc) {
            $.ajax({
                type: "GET",
                url: svr,
                dataType: "json",
                data: paras,
                cache: true,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        },
        syncGet: function(svr, paras, succFunc, errorFunc) {
            $.ajax({
                type: "GET",
                url: svr,
                dataType: "json",
                data: paras,
                cache: true,
                async: false,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        },
        getDataType: function (svr, paras,dataType, succFunc, errorFunc) {
            $.ajax({
                type: "GET",
                url: svr,
                dataType: dataType,
                data: paras,
                cache: true,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        },
        put: function(svr, paras, succFunc, errorFunc) {
            paras._method='PUT';
            $.ajax({
                type: "POST",
                url: svr,
                dataType: "json",
                // contentType:"application/json",
                data: paras,
                // data: JSON.stringify(paras),
                cache: true,
                async: false,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        },
        delete: function(svr, paras, succFunc, errorFunc) {
            paras._method='DELETE';
            $.ajax({
                type: "POST",
                url: svr,
                // headers : {
                //     'Content-Type' : 'application/json;charset=utf-8'
                // },
                dataType: "json",
                data: paras,
                cache: true,
                async: false,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        },
        deleteDuplicate: function(svr, paras, succFunc, errorFunc) {
            paras._method='DELETE';
            $.ajax({
                type: "POST",
                url: svr,
                // headers : {
                //     'Content-Type' : 'application/json;charset=utf-8'
                // },
                dataType: "json",
                data: paras,
                cache: true,
                async: false,
                success: function (data, paras) {
                    var ret = data;
                    succFunc(ret, paras);
                },
                error: function () {
                    errorFunc();
                }
            })
        }
    };
    exports('baajax', baajax);
});